package org.zpmis.api.business;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.ResultCode;
import org.zpmis.domain.org.enums.SysOrganizationEnum;
import org.zpmis.domain.org.model.SysOrganization;
import org.zpmis.domain.org.qo.AddOrgQo;
import org.zpmis.domain.org.qo.ChangeOrgStatusQo;
import org.zpmis.domain.org.qo.EditOrgQo;
import org.zpmis.domain.org.qo.OrgListQo;
import org.zpmis.domain.org.service.SysOrganizationService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.zpmis.domain.org.vo.AllSysOrgVo;
import org.zpmis.domain.org.vo.DeptTreeVo;
import org.zpmis.util.TreeUtil;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with CodeGenerator
 * Description:
 * @author  Yang
 * Date: 2021-6-2
 * Time: 14:45:18
 */
@Service
@Slf4j
public class SysOrganizationWebService {
     @Reference
     private SysOrganizationService sysOrganizationService;

    public List<DeptTreeVo> orgList(OrgListQo orgListQo) throws Exception{
        String orgCode = orgListQo.getOrgCode();
        String orgName = orgListQo.getOrgName();
        Integer orgLevel = orgListQo.getOrgLevel();
        Integer status = orgListQo.getStatus();
        Condition condition = new Condition(SysOrganization.class);
        Example.Criteria criteria = condition.createCriteria();
        if(StringUtils.isNotBlank(orgCode)){
            criteria.andLike("orgCode","%"+orgCode+"%");
        }
        if(StringUtils.isNotBlank(orgName)){
            criteria.andLike("orgName","%"+orgName+"%");
        }
        if(orgLevel != null){
            criteria.andEqualTo("orgLevel",orgLevel);
        }
        if(status != null){
            criteria.andEqualTo("status",status);
        }
        List<SysOrganization> orgList = sysOrganizationService.findByCondition(condition);
        //PageHelper.startPage(orgListQo.getPageNum(),orgListQo.getPageSize());
        //PageInfo pageInfo = new PageInfo<>(orgList);
        //设置自定义返回对象
        //List<OrgListVo> resultList = orgList.stream().map(OrgListVo::new).collect(Collectors.toList());
        //pageInfo.setList(resultList);
        List<DeptTreeVo> listDept = getDeptTree(orgList);
        return  listDept;
    }

    public void addOrg(AddOrgQo addOrgQo) {
        Integer isParent = addOrgQo.getIsParent();
        //判断是顶级组织 默认上级ID是0  上级ID没有选择的时候 也默认是0 如果上级id没有的话 那么默认当前记录为顶级组织
        if(isParent == 1 || addOrgQo.getParentId() == null){
            addOrgQo.setParentId(0L);
            addOrgQo.setIsParent(1);
        }
        sysOrganizationService.save(SysOrganization.builder()
                .orgCode(addOrgQo.getOrgCode())
                .orgName(addOrgQo.getOrgName())
                .isParent(addOrgQo.getIsParent())
                .orgLevel(addOrgQo.getOrgLevel())
                .status(1)
                .parentId(addOrgQo.getParentId())
                .build()
        );
    }

    public void editOrg(EditOrgQo editOrgQo) {
        Long id = editOrgQo.getId();
        SysOrganization sysOrg = sysOrganizationService.findById(id);
        if(sysOrg == null){
            throw new BusinessException(ResultCode.DATA_NOT_FOUND);
        }
        SysOrganization editOrg = new SysOrganization();
        editOrg.setId(editOrgQo.getId());
        editOrg.setIsParent(editOrgQo.getIsParent());
        editOrg.setOrgCode(editOrgQo.getOrgCode());
        editOrg.setOrgName(editOrgQo.getOrgName());
        editOrg.setOrgLevel(editOrgQo.getOrgLevel());
        editOrg.setParentId(editOrgQo.getParentId());
        sysOrganizationService.update(editOrg);
    }

    public void changeOrgStatus(ChangeOrgStatusQo changeOrgStatusQo) {
        Long id = changeOrgStatusQo.getId();
        SysOrganization sysOrg = sysOrganizationService.findById(id);
        if(sysOrg == null){
            throw new BusinessException(ResultCode.DATA_NOT_FOUND);
        }
        SysOrganization sysOrganization = new SysOrganization().builder().id(id).status(sysOrg.getStatus()).build();
        Integer status = sysOrganization.getStatus();
        if(status == SysOrganizationEnum.ENABLE_STATUS.code()){
            sysOrganization.setStatus(SysOrganizationEnum.DISABLE_STATUS.code());
        }
        if(status == SysOrganizationEnum.DISABLE_STATUS.code()){
            sysOrganization.setStatus(SysOrganizationEnum.ENABLE_STATUS.code());
        }
        sysOrganizationService.update(sysOrganization);
    }

    public List<AllSysOrgVo> allSysOrg() {
        Condition condition = new Condition(SysOrganization.class);
        condition.createCriteria()
                .andEqualTo("status",1);
        List<SysOrganization> sysOrgList = sysOrganizationService.findByCondition(condition);
        List<AllSysOrgVo> resultList = sysOrgList.stream().map(AllSysOrgVo::new).collect(Collectors.toList());
        return resultList;
    }

    public List<DeptTreeVo> getDeptTree(List<SysOrganization> depts) {
        List<DeptTreeVo> treeList = depts.stream()
                .filter(dept -> !dept.getId().equals(dept.getParentId()))
                .map(dept -> {
                    DeptTreeVo node = new DeptTreeVo();
                    node.setId(dept.getId());
                    node.setParentId(dept.getParentId());
                    node.setOrgName(dept.getOrgName());
                    node.setOrgCode(dept.getOrgCode());
                    node.setIsParent(dept.getIsParent());
                    node.setOrgLevel(dept.getOrgLevel());
                    node.setStatus(dept.getStatus());
                    return node;
                }).collect(Collectors.toList());
        return TreeUtil.bulid(treeList, 0l);
    }
}
